// noinspection JSIgnoredPromiseFromCall,DuplicatedCode,ES6PreferShortImport

import { app, BrowserWindow, BrowserView, protocol, globalShortcut } from 'electron';
import installExtension, { VUEJS3_DEVTOOLS } from 'electron-devtools-installer';
import { ACHEME, getRenderContentUrl } from './config/run';

const { createWindow } = require('./window/createWindow');
// const { ipc, BaseWindow } = require('../lib/sugar-electron');
import { initWorkbenchApplicationIpcMain } from './workbench-application/main';

// const { ipc, BaseWindow } = require('sugar-electron');

// BaseWindow.setDefaultOption({
//   webPreferences: {
//     nodeIntegration: true,
//     enableRemoteModule: true,
//     contextIsolation: false,
//   },
// });

global.mainMethods = {
  createWindow,
};

const isDevelopment = process.env.NODE_ENV !== 'production';

protocol.registerSchemesAsPrivileged([
  {
    scheme: ACHEME,
    privileges: {
      secure: true,
      standard: true,
    },
  },
]);

async function createMainWindow() {
  const mainWindow = new BrowserWindow({
    width: 1200,
    height: 900,
    show: true,
    webPreferences: {
      enableRemoteModule: true,
      nodeIntegration: true,
      contextIsolation: false,
      webSecurity: false,
    },
  });
  mainWindow.loadURL(getRenderContentUrl('/window-event'));
}

app.on('ready', async () => {
  if (isDevelopment && !process.env.IS_TEST) {
    try {
      await installExtension(VUEJS3_DEVTOOLS);
    } catch (e) {
      console.error('Vue Devtools 安装失败', e.toString());
    }
  }
  createMainWindow();
  globalShortcutInit();
  initWorkbenchApplicationIpcMain();
});

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit();
  }
});

app.on('activate', () => {
  if (!BrowserWindow.getAllWindows().length) {
    createMainWindow();
  }
});

function globalShortcutInit() {
  globalShortcut.register('Ctrl+Shift+i', () => {
    const win = BrowserWindow.getFocusedWindow();
    win && win.webContents.toggleDevTools();
  });
}

BrowserView;
